package com.utils.updateapp;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jinlejia.oldmodule.R;


/**
 * @author SongJiaTai
 * @description:版本升级更新提示框
 * @date :2019/5/23 18:06
 */
public class MandatoryUpdateDialog extends AlertDialog{
    private String TAG = "MandatoryUpdateDialog";
    private String title,content,cancel,confirm;
    private int tag;
    protected MandatoryUpdateDialog(Context context) {
        super(context);
    }

    public MandatoryUpdateDialog(Context context, String title, String content, String cancel, String confirm,int tag) {
        super(context);
        this.title = title;
        this.content = content;
        this.cancel = cancel;
        this.confirm = confirm;
        this.tag = tag;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = this.getWindow();
        this.setCanceledOnTouchOutside(false);
        if (tag==1){
            window.setContentView(R.layout.mandatoryupdatedialoglayout);
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable());
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //Title
            TextView titleText = window.findViewById(R.id.title);
            titleText.setText(title);
            //content
            TextView contentText = window.findViewById(R.id.content);
            contentText.setText(content);
            Log.e(TAG, "onCreate: "+content.length());
            if (content.length()>50){
                contentText.setHeight(300);
            }

            contentText.setMovementMethod(ScrollingMovementMethod.getInstance());

            //取消
            TextView cancleTxt = (TextView) window.findViewById(R.id.id_cancel);
            if(!TextUtils.isEmpty(cancel))
                cancleTxt.setText(cancel);
            //确定
            TextView confirmTxt = (TextView) window.findViewById(R.id.id_confirm);
            if( !TextUtils.isEmpty( confirm ) )
                confirmTxt.setText(confirm);

            cancleTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                    if (null != cancelListener){
                        cancelListener.cancel();
                    }
                }
            });
            confirmTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                    if( null != confirmListener )
                        confirmListener.confirm();
                }
            });
        }else{
            window.setContentView(R.layout.noticedialoglayout);
            window.setGravity(Gravity.CENTER);
            window.setBackgroundDrawable(new ColorDrawable());
            window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //Title
            TextView titleText = window.findViewById(R.id.title);
            titleText.setText(title);
            //content
            TextView contentText = window.findViewById(R.id.content);
            contentText.setText(content);
            Log.e(TAG, "onCreate: "+content.length());
            if (content.length()>50){
                contentText.setHeight(300);
            }
            contentText.setMovementMethod(ScrollingMovementMethod.getInstance());
            //确定
            TextView confirmTxt = (TextView) window.findViewById(R.id.id_confirm);
            if( !TextUtils.isEmpty( confirm ) )
                confirmTxt.setText(confirm);

            confirmTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dismiss();
                    if( null != confirmListener )
                        confirmListener.confirm();
                }
            });
        }

    }


    public interface OnConfirmListener {
        void confirm();
    }
    public interface OnCancelListener{
        void cancel();
    }

    private OnConfirmListener confirmListener;

    public void setConfirmListener(OnConfirmListener listener) {
        this.confirmListener = listener;
    }

    private OnCancelListener cancelListener;
    public void setCancelListener(OnCancelListener listener){
        this.cancelListener = listener;
    }

}

